package me.firstapp.api.shop;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.ShoppingCart;
import me.firstapp.service.shop.ShoppingCartItemService;
import me.firstapp.service.shop.ShoppingCartService;

@Controller
@RequestMapping(value = "/shop")
@Api(value = "/shop", description = "购物相关接口")
public class ShopApi extends BaseApi {

	@Autowired
	private ShoppingCartItemService shoppingCartItemService;

	@Autowired
	private ShoppingCartService shoppingCartService;

	// params格式：[{"productId":1, "productSkuRelId":1 "counts":1},{"productId":1,
	// "productSkuRelId":1, "counts":1}]
	@RequestMapping(value = "/updateShoppingCart.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新购物车", notes = "更新购物车")
	public void updateShoppingCart(@RequestBody Map<String, Object> body, Long shoppingCartId,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String params = (String) body.get("params");
			if (StrUtils.isNULL(params)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "json参数");
			}
			JSONArray jsonArray = JSONArray.parseArray(params);
			if (jsonArray != null) {
				for (int i = 0; i < jsonArray.size(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					if (jsonObject != null) {
						Long productId = jsonObject.getLong("productId");
						Integer counts = jsonObject.getInteger("counts");
						Long productSkuRelId = jsonObject.getLong("productSkuRelId");
						shoppingCartItemService.addUpdateShoppingCartItem(shoppingCartId, productId, productSkuRelId,
								counts, apiClientId);
					}
				}
			}

			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteShoppingCartProduct.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除购物车中的商品", notes = "删除购物车中的商品")
	public void deleteShoppingCartProduct(Long shoppingCartId, String productIds, String productSkuRelIds,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			shoppingCartItemService.deleteShoppingCartItems(shoppingCartId, productIds, productSkuRelIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findMemberShoppingCart.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户购物车", notes = "获取用户购物车")
	public void findMemberShoppingCart(Long memberId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<ShoppingCart> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			ShoppingCart shoppingCart = shoppingCartService.findMemberShoppingCart(memberId, true, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(shoppingCart);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
